package com.x.mvp.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

public class AnimationUtils {
    public static RotateAnimation rotate0To180() {
        RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(250);
        animation.setFillAfter(true);
        return animation;
    }

    public static RotateAnimation rotate180To0() {
        RotateAnimation animation = new RotateAnimation(180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(250);
        animation.setFillAfter(true);
        return animation;
    }

    public static RotateAnimation justRotate0To180() {
        RotateAnimation animation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(0);
        animation.setFillAfter(true);
        return animation;
    }

    public static void animHeight(final View view, final int start, final int end, final boolean isToShow,
                                  long duration) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        final ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        valueAnimator.addUpdateListener((animation) -> {
            int height = (int) animation.getAnimatedValue();
            layoutParams.height = height;
            view.setLayoutParams(layoutParams);
            view.requestLayout();
        });

        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                if (isToShow) {
                    view.setVisibility(View.VISIBLE);
                }
                super.onAnimationStart(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (!isToShow) {
                    view.setVisibility(View.GONE);
                }
            }
        });
        valueAnimator.setDuration(duration);
        valueAnimator.start();
    }

    public static void animHeight(Activity context, View view, boolean isToShow, long duration) {
        int[] sreen = DensityUtil.getScreenWidthAndHeight(context);
        int end = sreen[1];
        if (isToShow) {
            int width = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            int height = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            view.measure(width, height);
            end = end > view.getMeasuredHeight() ? view.getMeasuredHeight() : end;
            animHeight(view, 0, end, isToShow, duration);
        } else {
            animHeight(view, view.getLayoutParams().height, 0, isToShow, duration);
        }
    }

    public static void animWidth(Activity context, View view, boolean isToShow, long duration) {
        int[] size = DensityUtil.getScreenWidthAndHeight(context);
        int end = size[0];
        if (isToShow) {
            Display display = context.getWindowManager().getDefaultDisplay();
            animWidth(view, 0, end, isToShow, duration);
        } else {
            animWidth(view, 0, end, isToShow, duration);
        }
    }

    public static void animWidth(final View view, final int start, final int end, final boolean isToShow,
                                 long duration) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        final ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        valueAnimator.addUpdateListener((animation) -> {
            int width = (int) animation.getAnimatedValue();
            if (isToShow) {
                layoutParams.setMargins(0 - end + width, 0, end - width, 0);
            } else {
                layoutParams.setMargins(0 - width, 0, width, 0);
            }
            view.setLayoutParams(layoutParams);
            view.requestLayout();
        });

        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                if (isToShow) {
                    view.setVisibility(View.VISIBLE);
                }
                super.onAnimationStart(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (!isToShow) {
                    view.setVisibility(View.GONE);
                }
            }
        });
        valueAnimator.setDuration(duration);
        valueAnimator.start();
    }
}